package com.gitee.l0km.jsonvisitor;

import static org.junit.Assert.*;

import java.io.File;

import org.antlr.v4.runtime.Token;
import org.junit.FixMethodOrder;

import static net.gdface.utils.SimpleLog.log;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import com.alibaba.fastjson.parser.JSONLexer;
import com.alibaba.fastjson.parser.JSONToken;
import com.gitee.l0km.jsonvisitor.fastjson.Fastjsons;
import com.gitee.l0km.jsonvisitor.fastjson.FastjsonPairMatcher0;

import net.gdface.utils.BinaryUtils;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FastjsonPairTest0 {
	@Test
	public void test1NextToken() {
		try {
			{
				File file = new File("parserTest.json");
				final byte[] jsonBytes = BinaryUtils.getBytes(file);
				JSONLexer lexer = Fastjsons.createJSONLexer(jsonBytes);
				do {
					lexer.nextToken();
					int t = lexer.token();
					if(t==JSONToken.LITERAL_STRING) {
						log("{}:{}", JSONToken.name(t), lexer.stringVal());
					}else {
						log("{}:{}", t, JSONToken.name(t));
					}
				} while (lexer.token() != JSONToken.EOF);
			}

		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}
	@Test
	public void test2MatchPair() {
		try {
			{
				File file = new File("parserTest.json");
				String matched = FastjsonPairMatcher0.matchPair(file);
				log("matched [{}]",matched);
				assertNotNull(matched);
				assertTrue(matched.startsWith("TOKEN:"));
			}
			{
				String jsonstr = "[]";
				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
				log("matched [{}]",matched);
				assertNull(matched);
			}
			{
				String jsonstr = "{\"tokenId\":0}";
				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
				log("matched [{}]",matched);
				assertNull(matched);
			}
			{
				String jsonstr = "{\"tokenId\":\"world\"}";
				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
				log("matched [{}]",matched);
				assertEquals("world",matched);
			}
			{
				String jsonstr = "";
				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
				log("matched [{}]",matched);
				assertNull(matched);
			}
			{
				String jsonstr = "\"hello\"";
				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
				log("matched [{}]",matched);
				assertNull(matched);
			}
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}
	@Test
	public void test3MatchPairObj() {
		try {
			{
				File file = new File("parserTest.obj.json");
				String matched = FastjsonPairMatcher0.matchPair(file);
				log("matched [{}]",matched);
				assertNotNull(matched);
				assertTrue(matched.indexOf("TOKEN:")>0);
			}
//			{
//				String jsonstr = "[]";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
//			{
//				String jsonstr = "{\"tokenId\":0}";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
//			{
//				String jsonstr = "{\"tokenId\":\"world\"}";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertEquals("world",matched);
//			}
//			{
//				String jsonstr = "";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
//			{
//				String jsonstr = "\"hello\"";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}
	@Test
	public void test4MatchPairArr() {
		try {
			{
				File file = new File("parserTest.arr.json");
				String matched = FastjsonPairMatcher0.matchPair(file);
				log("matched [{}]",matched);
				assertNotNull(matched);
				assertTrue(matched.indexOf("TOKEN:")>0);
			}
//			{
//				String jsonstr = "[]";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
//			{
//				String jsonstr = "{\"tokenId\":0}";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
//			{
//				String jsonstr = "{\"tokenId\":\"world\"}";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertEquals("world",matched);
//			}
//			{
//				String jsonstr = "";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
//			{
//				String jsonstr = "\"hello\"";
//				String matched = FastjsonPairMatcher0.matchPair(jsonstr);
//				log("matched [{}]",matched);
//				assertNull(matched);
//			}
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}

}
